# -*- coding: utf-8 -*-

from gi.repository import Gtk
from gi.repository import GObject

from pygtkcompat.generictreemodel import GenericTreeModel

import sys

from model.toquin import Toquin
from model.toquinitem import ToquinItem

class ToquinModelGtk(GenericTreeModel):

    def __init__(self, toquinmodel, filter = None):
        GenericTreeModel.__init__(self)
        self.toquinmodel = toquinmodel

        if filter:
            if filter == "marked":
                self.__values = toquinmodel.getMarkedForInstallation()
            elif filter.startswith("category:"):
                category = filter.split(":")[1]
                self.__values = toquinmodel.getCategory(category)
            elif filter.startswith("kit:"):
                kit = filter.split(":")[1]
                self.__values = toquinmodel.getKit(kit)
        else:
            self.__values = toquinmodel.getItems()

        self.__iters = range(0, len(self.__values))
        self.props.leak_references = False

    def on_get_flags(self):
        return Gtk.TreeModelFlags.LIST_ONLY | Gtk.TreeModelFlags.ITERS_PERSIST

    def on_get_n_columns(self):
        return 4

    def on_get_column_type(self, index):
        if index == 0:
            return bool
        elif index < self.on_get_n_columns():
            return str

        raise IndexError
    
    def on_get_value(self, row, column):
        if column == 0:
            return self.toquinmodel.isMarkedForInstallation(self.__values[row])
        else:
            # Shouldn't we be able to return unicode here?
            if column == 1:
                if self.__values[row].name:
                    return self.__values[row].name.encode("utf-8")
                else:
                    return ""
            if column == 2:
                if self.__values[row].keywords:
                    return self.__values[row].keywords.encode("utf-8")
                else:
                    return ""

        raise IndexError
                
    def on_get_iter(self, path):
        try:
            return self.__iters[path[0]]
        except Exception:
            return None

    def on_get_path(self, row):
        return (row,)

    def on_iter_parent(self, row):
        return None

    def on_iter_next(self, row):
        if row + 1 < len(self.__values):
            return self.__iters[row + 1]
        else:
            return None

    def on_iter_children(self, row):
        if row is None:
            return len(self.__values)
        else:
            return 0

    def on_iter_nth_child(self, parent, n):
        if parent:
            return None
        else:
            return self.__iters[n]
        
    def on_iter_has_child(self, parent):
        return False
        
    def __len__(self):
        return len(self.__values)
    
    def __getitem__(self, key):
        return self.__values[self.get_user_data(key)]
    
    def __iter__(self):
        return iter(self.__values)

    def __contains__(self, value):
        return value in self.__values

    def markForInstallation(self, cellrenderer, path):
        itemindex = int(path)

        if self.toquinmodel.isMarkedForInstallation(self.__values[itemindex]):
            self.toquinmodel.unmarkForInstallation(self.__values[itemindex])
        else:
            self.toquinmodel.markForInstallation(self.__values[itemindex])
